package com.xxl.job.admin.repository;

import com.xxl.job.admin.core.conf.XxlJobAdminConfig;
import com.xxl.job.admin.core.enums.DatabaseTypeEnum;
import com.xxl.job.admin.core.model.XxlJobLogReport;
import com.xxl.job.admin.dao.XxlJobLogReportDao;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public class XxlJobLogReportRepository implements XxlJobLogReportDao {
    private final XxlJobLogReportDao xxlJobLogReportDao;

    public XxlJobLogReportRepository(XxlJobLogReportDao xxlJobLogReportDao) {
        this.xxlJobLogReportDao = xxlJobLogReportDao;
    }

    @Override
    public int save(XxlJobLogReport xxlJobLogReport) {
        DatabaseTypeEnum typeEnum = DatabaseTypeEnum.getByType(XxlJobAdminConfig.getAdminConfig().getDatabaseId());
        switch (typeEnum) {
            case MYSQL:
                return xxlJobLogReportDao.save(xxlJobLogReport);
            case ORACLE:
                // 从Oracle序列中获取ID并设值
                xxlJobLogReport.setId(xxlJobLogReportDao.getNextOracleSequenceId());
                return xxlJobLogReportDao.saveWithId(xxlJobLogReport);
            default:
                throw new RuntimeException("un support database type");
        }
    }

    @Override
    public int getNextOracleSequenceId() {
        return xxlJobLogReportDao.getNextOracleSequenceId();
    }

    @Override
    public int saveWithId(XxlJobLogReport xxlJobLogReport) {
        return xxlJobLogReportDao.saveWithId(xxlJobLogReport);
    }

    @Override
    public int update(XxlJobLogReport xxlJobLogReport) {
        return xxlJobLogReportDao.update(xxlJobLogReport);
    }

    @Override
    public List<XxlJobLogReport> queryLogReport(Date triggerDayFrom, Date triggerDayTo) {
        return xxlJobLogReportDao.queryLogReport(triggerDayFrom, triggerDayTo);
    }

    @Override
    public XxlJobLogReport queryLogReportTotal() {
        return xxlJobLogReportDao.queryLogReportTotal();
    }
}
